package com.neoCalf.neoUser.webSocket.handler;

import com.neoCalf.neoCommon.model.dto.JoinGroupDto;
import com.neoCalf.neoCommon.model.dto.SendMessageDto;
import com.neoCalf.neoCommon.utils.DataTool;
import com.neoCalf.neoCommon.utils.SendUtils;
import com.neoCalf.neoUser.webSocket.tool.ChannelContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 分布式消息发送类
 */
@Component(value = "handlerMessage")
@Slf4j
public class HandlerMessage {
    @Resource
    ChannelContextUtils channelContextUtils;
    @Resource
    SendUtils sendUtils;

    public void ListenerMessage() {
        sendUtils.ListenerMessage(SendMessageDto.class, (SendMessageDto, sendDto) -> {
            if (!DataTool.isNull(sendDto)) {
                try {
                    channelContextUtils.sendMessageMethod(sendDto);
                } catch (Exception e) {
                    log.error("消息发送失败", e);
                }
            }
        });
    }

    public void addChannelByUserIdToGroup() {
        sendUtils.ListenerJoinGroup(JoinGroupDto.class, (JoinGroupDto, joinGroupDto) -> {
            if (!DataTool.isNull(joinGroupDto)) {
                try {
                    channelContextUtils.addChannelByUserIdToGroup(joinGroupDto.getGroupId(), joinGroupDto.getUserId());
                } catch (Exception e) {
                    log.error("加入群聊失败", e);
                }
            }
        });
    }
}
